package com.cccyy.generate.idcard;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.cccyy.generate.idcard.Windows.*;

public class CardGen {
    private static final String basePath = "/idcard/base_pic/";

    public static void main(String[] args) {
        try {
            help();
            Scanner sc = new Scanner(System.in);
            String arg = "";
//            while (!arg.equals("exit")) {
            System.out.println("请输入需要生成身份证照片的数量（正反面一起算一份）...");
            arg = sc.nextLine();
            if ("-help".equals(arg)) {
                help();
                arg = sc.nextLine();
            } else if ("exit".equals(arg)) {
                System.out.println("\n");
                int c = 0;
                String[] wel = {"谢", "谢", "使", "用", "!", "", "", "再", "见", "!"};
                while (c < 10) {
                    Thread.sleep(50);
                    if (c < 4) {
                        System.out.print(wel[c]);
                    } else if (c > 4 && c < 7) {
                        System.out.println();
                    } else {
                        System.out.print(wel[c]);
                    }
                    c++;
                }
                System.exit(-1);
            } else {
                int num = parseInt(arg, 1);

                Windows wm = new Windows();
                //TODO 使用相对路径存在问题
//                String url = "D:\\WorkSpace\\Study\\personal-tools\\src\\main\\resources\\base_pic\\idf.jpg";
                String url = "src/main/resources/idcard/base_pic/idf.jpg";
//                url = Util.StringDecode(url);
//                String baseDir = "/idcard/base_pic/";
                String baseDir = getJarDir(url);
//                createDir(basePath + "\\images\\");
                createDir(baseDir + "\\images\\");
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                String moment = format.format(new Date());
//                String newDir = basePath + "\\idcode\\" + moment + "\\";
                String newDir = baseDir + "\\idcode\\" + moment + "\\";
                createDir(newDir);
//                    System.out.println("请输入需要要保存的路径（默认保存当前路径，若要默认直接按回车即可）...");
//                    arg = sc.nextLine();
//                    if (args.length > 1) {
//                        newDir = args[1];
//                    } else {
//                        createDir(newDir);
//                    }

                Map<String, String> params = new HashMap<>();

                System.out.println("请输入身份证姓名：");
                String name = sc.nextLine();
//                String name = ChineseName.generateName();
                params.put("name", name);

                System.out.println("请输入性别（0-男；1-女）：");
                String sex = sc.nextLine();
                //根据性别筛选图片合成
//                checkImage(wm, basePath);
                checkImage(wm, baseDir);

//                    System.out.println("请输入身份证号码：");
//                    String code = sc.nextLine();
                Random random = new Random();
                int month = random.nextInt(12) + 1;
                int day = random.nextInt(28) + 1;


                String str = "1992-" + month + "-" + day;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = sdf.parse(str);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);


                String code = IdGen.generate(calendar, sex);//0-男；1-女
                params.put("code", code);

//                    System.out.println("请输入民族：");
//                    String nation = sc.nextLine();
                params.put("nation", "汉");

//                    System.out.println("住址：");
//                    String address = sc.nextLine();
                params.put("address", "北京市丰台区成寿寺路甲19号");

//                    System.out.println("请输入签发机关：");
//                    String officeAddress = sc.nextLine();
                params.put("officeAddress", "北京市公安局");

//                    System.out.println("请输入有效期限：（格式为：yyyy.mm.dd-yyyy.mm.dd）");
//                    String validityPeriod = sc.nextLine();
//                    String validityPeriod = "2020.04.15-2040.04.15";
                String vSat = "2020";
                String vEnd = "2040";
                if (month < 10) {
                    vSat = vSat + ".0" + month;
                    vEnd = vEnd + ".0" + month;
                } else {
                    vSat = vSat + "." + month;
                    vEnd = vEnd + "." + month;
                }

                if (day < 10) {
                    vSat = vSat + ".0" + day;
                    vEnd = vEnd + ".0" + day;
                } else {
                    vSat = vSat + "." + day;
                    vEnd = vEnd + "." + day;
                }
                params.put("validityPeriod", vSat + "-" + vEnd);

                //D:\WorkSpace\Study\study-test\src\main\resources\idcard\base_pic    baseDir
                generateIdcodeZ(baseDir, newDir, params);
//                generateIdcodeZ(basePath, newDir, params);

                System.out.println("\n☆☆☆☆☆☆☆☆ 完成 ☆☆☆☆☆☆☆☆\n");
            }
//            }
        } catch (InterruptedException | ParseException e) {
            e.printStackTrace();
        }
    }
}
